package com.doge.service.wx.comment;

import com.alibaba.druid.util.StringUtils;
import com.doge.bean.base.BaseData;
import com.doge.bean.base.BaseParam;
import com.doge.bean.pojo.comment.Comment;
import com.doge.bean.pojo.comment.CommentExample;
import com.doge.bean.pojo.user.User;
import com.doge.bean.pojo.user.UserExample;
import com.doge.bean.vo.comment.CommentVO;
import com.doge.bean.vo.comment.CountVO;
import com.doge.bean.vo.comment.UserInfo;
import com.doge.mapper.CommentMapper;
import com.doge.mapper.UserMapper;
import com.doge.util.auth.GetSubject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * showType:显示图片 0 hasPicture=false 不显示图片
 */
@Service
public class WxCommentServiceImpl implements WxCommentService{
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public BaseData listComment(BaseParam baseParam, Integer valueId, Byte type, Integer showType) {
        PageHelper.startPage(baseParam.getPage(),baseParam.getLimit());
        CommentExample example = new CommentExample();
        CommentExample.Criteria criteria = example.createCriteria();
        //将前端的删选条件放入,valueId固定住
        criteria.andValueIdEqualTo(valueId).andDeletedEqualTo(false);
        if (showType == 1){
            criteria.andHasPictureEqualTo(true);
        }
        List<Comment> comments = commentMapper.selectByExample(example);
        List<Integer> userIds = new ArrayList<>();
        if (comments.size()==0){
            BaseData data = BaseData.list(null, 0);
            data.setPages(0);
            data.setPage(baseParam.getPage());
            data.setLimit(baseParam.getLimit());
            return data;
        }
        for (Comment comment : comments) {
            Integer userId = comment.getUserId();
            userIds.add(userId);
        }
        UserExample userExample = new UserExample();
        UserExample.Criteria userCriteria = userExample.createCriteria();
        userCriteria.andIdIn(userIds);
        List<User> users = userMapper.selectByExample(userExample);
        List<CommentVO> commentList = new ArrayList<>();
        //将userinfo相关信息存储
        //将comment相关信息存储 双层for循环
        for (Comment comment : comments) {
            for (User user : users) {
                CommentVO commentVO = new CommentVO();
                String avatarUrl = user.getAvatar();
                String nickname = user.getNickname();
                UserInfo userInfo = new UserInfo();
                if (!StringUtils.isEmpty(avatarUrl)){
                    userInfo.setAvatarUrl(avatarUrl);
                }
                if (!StringUtils.isEmpty(nickname)){
                    userInfo.setNickName(nickname);
                }
                commentVO.setUserInfo(userInfo);

                Date addTime = comment.getAddTime();
                String adminContent = comment.getAdminContent();
                String content = comment.getContent();
                String[] picUrls = comment.getPicUrls();

                if (addTime!=null){
                    commentVO.setAddTime(addTime);
                }
                if (adminContent !=null){
                    commentVO.setAdminContent(adminContent);
                }
                if (content!=null){
                    commentVO.setContent(content);
                }
                if (picUrls!=null){
                    commentVO.setPicList(picUrls);
                }
                commentList.add(commentVO);
            }
        }
        PageInfo pageInfo = new PageInfo(commentList);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        BaseData data = BaseData.list(commentList, total);
        data.setPages(pages);
        data.setLimit(baseParam.getLimit());
        data.setPage(baseParam.getPage());
        return data;
    }

    @Override
    public Comment postComment(Comment comment) {
        comment.setAddTime(new Date());
        comment.setUpdateTime(new Date());
        Integer userId = GetSubject.getUserId();
        comment.setUserId(userId);
        //在数据库里插入刚刚生成的评论
        int rows = commentMapper.insertSelective(comment);
        CommentExample example = new CommentExample();
        CommentExample.Criteria criteria = example.createCriteria();
        //根据留言内容查找，回显那条留言的内容
        criteria.andContentEqualTo(comment.getContent());
        List<Comment> comments = commentMapper.selectByExample(example);
        return comments.get(0);
    }

    @Override
    public CountVO count(Integer valueId, Byte type) {
        CommentExample example = new CommentExample();
        CommentExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false).andValueIdEqualTo(valueId);
        long allComment = commentMapper.countByExample(example);
        criteria.andHasPictureEqualTo(true);
        long hasPic = commentMapper.countByExample(example);
        CountVO countVO = new CountVO();
        countVO.setAllCount(allComment);
        countVO.setHasPicCount(hasPic);
        return countVO;
    }

}